<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="反馈时间" style="width: 428px;">
        <el-date-picker v-model="dateRange" value-format="YYYY-MM-DD HH:mm:ss" type="datetimerange" :default-time="[new Date(2000, 1, 1, 0, 0, 0),
        new Date(2000, 2, 1, 23, 59, 59),]" range-separator="-" start-placeholder="开始时间"
          end-placeholder="结束时间"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table highlight-current-row v-loading="loading" :data="feedbackList" @selection-change="handleSelectionChange">
      <el-table-column width="600" label="问题/意见描述" prop="feedbackDetails" align="left" show-overflow-tooltip>
      </el-table-column>
      <el-table-column :label="v.name" align="left" :prop="v.prop" v-for="v in tableItems" :width="v.width">
        <template #default="scope">
          <span>{{ scope.row[v.prop] }}</span>
        </template>
      </el-table-column>
      <el-table-column label="图片" align="left">
        <template #default="scope">
          <div v-if="scope.row.imageList && scope.row.imageList.length">
            <el-image style="height: 6rem;width: 100%;" fit="cover" preview-teleported="true" :src="getImgSrc(scope.row.imageList[0])"
              :preview-src-list="getImgSrc(scope.row.imageList)">
            </el-image>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改用户反馈对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="feedbackRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="联系号码" prop="phoneNumber">
          <el-input v-model="form.phoneNumber" placeholder="请输入联系号码" />
        </el-form-item>
        <el-form-item label="反馈详情" prop="feedbackDetails">
          <el-input v-model="form.feedbackDetails" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="创建用户id" prop="userIdCreate">
          <el-input v-model="form.userIdCreate" placeholder="请输入创建用户id" />
        </el-form-item>
        <el-form-item label="修改用户id" prop="userIdModified">
          <el-input v-model="form.userIdModified" placeholder="请输入修改用户id" />
        </el-form-item>
        <el-form-item label="修改时间" prop="modifiedTime">
          <el-date-picker clearable v-model="form.modifiedTime" type="date" value-format="YYYY-MM-DD"
            placeholder="请选择修改时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Feedback">
import { listFeedback, getFeedback, delFeedback, addFeedback, updateFeedback } from "@/api/comlbs/feedback";
import dayjs from "dayjs";

const { proxy } = getCurrentInstance();
const url =
  'https://fuss10.elemecdn.com/a/3f/3302e58f9a181d2509f3dc0fa68b0jpeg.jpeg'
const srcList = [
  'https://fuss10.elemecdn.com/a/3f/3302e58f9a181d2509f3dc0fa68b0jpeg.jpeg',
  'https://fuss10.elemecdn.com/1/34/19aa98b1fcb2781c4fba33d850549jpeg.jpeg',
  'https://fuss10.elemecdn.com/0/6f/e35ff375812e6b0020b6b4e8f9583jpeg.jpeg',
  'https://fuss10.elemecdn.com/9/bb/e27858e973f5d7d3904835f46abbdjpeg.jpeg',
  'https://fuss10.elemecdn.com/d/e6/c4d93a3805b3ce3f323f7974e6f78jpeg.jpeg',
  'https://fuss10.elemecdn.com/3/28/bbf893f792f03a54408b3b7a7ebf0jpeg.jpeg',
  'https://fuss10.elemecdn.com/2/11/6535bcfb26e4c79b48ddde44f4b6fjpeg.jpeg',
]
const feedbackList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const dateRange = ref([dayjs(new Date()).format("YYYY-MM-DD 00:00:00"), dayjs(new Date()).format("YYYY-MM-DD 23:59:59")]);
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 15,
  },
  rules: {
    userId: [
      { required: true, message: "用户id不能为空", trigger: "blur" }
    ],
    isDel: [
      { required: true, message: "是否删除0否1是不能为空", trigger: "blur" }
    ]
  }
});
const tableItems = ref([
  { name: "反馈用户", prop: "createUserName" },
  { name: "联系方式", prop: "phoneNumber" },
  { name: "反馈时间", prop: "createTime" },
])
const { queryParams, form, rules } = toRefs(data);

/** 查询用户反馈列表 */
function getList() {
  loading.value = true;
  queryParams.value.startTime = dateRange.value ? dateRange.value[0] : ''
  queryParams.value.endTime = dateRange.value ? dateRange.value[1] : ''
  listFeedback(queryParams.value).then(response => {
    feedbackList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
function getImgSrc(val) {
  if (val && val.path) {
    return import.meta.env.VITE_APP_BASE_API + val.path;
  } else if (val && val.length > 0) {
    let picArr = []
    val.forEach(v => {
      picArr.push(import.meta.env.VITE_APP_BASE_API + v.path)
    });
    return picArr
  } else {
    return ""
  }
}
// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    userId: null,
    phoneNumber: null,
    feedbackDetails: null,
    imageList: null,
    userIdCreate: null,
    createTime: null,
    userIdModified: null,
    modifiedTime: null,
    isDel: null
  };
  proxy.resetForm("feedbackRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  queryParams.value.startTime = dateRange.value ? dateRange.value[0] : ''
  queryParams.value.endTime = dateRange.value ? dateRange.value[1] : ''
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  dateRange.value = [dayjs(new Date()).format("YYYY-MM-DD 00:00:00"), dayjs(new Date()).format("YYYY-MM-DD 23:59:59")];
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加用户反馈";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getFeedback(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改用户反馈";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["feedbackRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateFeedback(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addFeedback(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  let length = row.id ? 1 : _ids.length
  proxy.$modal.confirm('是否确认删除这' + length + '项数据？').then(function () {
    return delFeedback(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('comlbs/feedback/export', {
    ...queryParams.value
  }, `feedback_${new Date().getTime()}.xlsx`)
}

getList();
</script>
